import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2024-05-17
 * Time: 14:27
 */
public class Test {
    public static boolean singleNum(int[] array,int[] num) {
        for (int i = 2; i < array.length; i++) {
            if(array[i-2] %2 != 0 && array[i-1] %2 != 0 && array[i] % 2 != 0){
                num[0] = array[i-2];
                num[1] = array[i-1];
                num[2] = array[i];
                return true;
            }
        }
        return false;
    }
    public static void main(String[] args) {
        /*
        给你一个整数数组arr请你判断数组中是否存在连续三个元素都是奇数的情况：
        如果存在,请返回true;否则,返回 false
         */
        int[] array = {1,2,4,5,6,7,3,9};
        int[] num = new int[3];
        boolean ret = singleNum(array,num);
        if(ret == false){
            System.out.println("不存在连续三个元素都是奇数的情况");
        }else {
            System.out.println("存在连续三个元素都是奇数的情况，即" + Arrays.toString(num));
        }
    }
    public static int element(int[] array) {
        Arrays.sort(array);
        return array[array.length/2];
    }
    public static void main21(String[] args) {
        /*
        给定一个大小为 n 的数组，找到其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。
        你可以假设数组是非空的，并且给定的数组总是存在多数元素。
         */
        int[] array = {3,2,3};
        int ret = element(array);
        System.out.println(ret);
    }
    public static int single(int[] array) {
        int sum = 0;
        for(int x : array) {
            sum ^= x;
        }
        return sum;
    }
    public static void main20(String[] args) {
        //给定一个非空整数数组，除了某个元素只出现一次以外，其余每个元素均出现两次。找出那个只出现了一次的元素。
        int[] array = {1,4,5,4,2,1,5};
        int ret = single(array);
        System.out.println(ret);
    }
    public static int[] add(int[] nums,int target) {
        int[] ret = new int[2];
        boolean flag = false;
        for (int i = 0; i < nums.length; i++) {
            for (int j = i+1; j < nums.length; j++) {
                if(nums[i] + nums[j] == target) {
                    ret[0] = i;
                    ret[1] = j;
                    flag = true;
                    break;
                }
            }
        }
        if (flag == false) {
            return null;
        }
        return ret;
    }

    public static void main19(String[] args) {
        /*
        给定一个整数数组 nums 和一个整数目标值 target，请你在该数组中找出 和为目标值 target 的那 两个 整数，并返回它们的数组下标。
        你可以假设每种输入只会对应一个答案。但是，数组中同一个元素在答案里不能重复出现。
         */
        int[] nums = {1,2,3,4,5};
        int target = 5;
        int[] ret = add(nums,target);
        System.out.println(Arrays.toString(ret));

    }
    public static void main18(String[] args) {
        //给定一个有序整型数组, 实现二分查找
        int[] array = {1,2,3,4,5,6};
        int index = binarySearch(array,4);
        System.out.println(index);
    }
    public static void exchange(int[] array) {
        int left = 0;
        int right = array.length-1;
        while(left < right) {
            if(array[right] % 2 != 0 ){
                int tmp = array[right];
                array[right] = array[left];
                array[left] = tmp;
                left++;
            }else if(array[right] % 2 == 0){
                right--;
            }
        }
    }
    public static void main17(String[] args) {
        /*
        调整数组顺序使得奇数位于偶数之前。调整之后，不关心大小顺序。
        如数组：[1,2,3,4,5,6]
        调整后可能是：[1, 5, 3, 4, 2, 6]
         */
        int[] array = {1,2,3,4,5,6};
        exchange(array);
        System.out.println(Arrays.toString(array));
    }
    public static void transform(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = array[i] * 2;
        }
    }
    public static void main16(String[] args) {
        /*
        实现一个方法 transform,以数组为参数,
        循环将数组中的每个元素乘以2,并设置到对应的数组元素上.
        例如 原数组为 {1, 2, 3}, 修改之后为 {2, 4, 6}
         */
        int[] array = {1,2,3};
        transform(array);
        System.out.println(Arrays.toString(array));
    }
    public static void main15(String[] args) {
        //二维数组-->特殊的一维数组
        /*int[][] array = new int[2][3];
        int[][] array1 = new int[][]{{2,3,4},{5,6,7}};
        int[][] array2 = {{1,2,3},{4,5,6}};
        System.out.println(Arrays.toString(array1));
        System.out.println(array1.length);
        System.out.println(Arrays.toString(array1[0]));
        System.out.println(array1[0].length);
        System.out.println(Arrays.toString(array1[1]));
        System.out.println(array1[1].length);
        System.out.println(Arrays.deepToString(array1));*/
        int[][] array = {{1,2,3},{4,5,6}};
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("=====");
        for(int[] tmp : array) {
            for(int x : tmp) {
                System.out.print(x + " ");
            }
            System.out.println();
        }
        System.out.println("=====");
        System.out.println(Arrays.deepToString(array));
    }
    public static void reverse(int[] array) {
        int left = 0;
        int right = array.length-1;
        while(left <right) {
            int tmp = array[left];
            array[left] = array[right];
            array[right] =tmp;
            left++;
            right--;
        }
        System.out.println(Arrays.toString(array));
    }
    public static void main14(String[] args) {
        //给定一个数组, 将里面的元素逆序排列.
        int[] array = {1,2,3,4,5};
        reverse(array);
    }
    public static void avg(int[] array) {
        int sum = 0;
        /*for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }*/
        for(int x:array){
            sum += x;
        }
        double avgNum = sum * 1.0 / array.length;
        System.out.println(avgNum);
    }
    public static void main13(String[] args) {
        //给定一个整型数组, 求平均值
        int[] array = {1,2,3,4,6};
        avg(array);
    }
    public static void main12(String[] args) {
        int[] array1 = {1,2,3,4,5};
        int[] array2 = {1,2,3,4,5};
        System.out.println(array1 == array2);//判断指向的对象是否相同
        //equalsArray判断两个数组里面值是否相等
        boolean equalsArray = Arrays.equals(array1,array2);
        System.out.println(equalsArray);

        int[] array3 = new int[array1.length];
        Arrays.fill(array3,2);
        System.out.println(Arrays.toString(array3));
        Arrays.fill(array3,1,3,3);
        System.out.println(Arrays.toString(array3));
    }
    public static int[] copyArray(int[] array) {
        int[] copy = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            copy[i] = array[i];
        }
        return copy;
    }
    public static void main11(String[] args) {
        //数组的拷贝
        int[] array = {1,2,3,4,5};
        int[] copy = copyArray(array);
        System.out.println(Arrays.toString(copy));
        int[] copy1 = Arrays.copyOf(array,array.length);
        System.out.println(Arrays.toString(copy1));
        int[] copy2 = Arrays.copyOf(array,array.length*2);
        System.out.println(Arrays.toString(copy2));
        int[] copy3 = Arrays.copyOfRange(array,1,3);//[1,3)
        System.out.println(Arrays.toString(copy3));

        int[] copy4 = new int[array.length];
        System.arraycopy(array,0,copy4,0,array.length);
        System.out.println(Arrays.toString(copy4));

    }
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j] >= array[j+1]) {
                    int tmp = array[j];
                    array[j] =array[j+1];
                    array[j+1] = tmp;
                    flag = true;
                }
            }
            if(flag == false) {
                return;
            }
        }
    }
    public static void main10(String[] args) {
        //冒泡排序
        int[] array = {6,5,3,4,1,3,5,2};
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
    }
    public  static int search(int[] array,int key) {
        for (int i = 0; i < array.length; i++) {
            if(key == array[i]){
                return i;
            }
        }
        return -1;
    }
    public static int binarySearch(int[] array,int key) {
        int left = 0;
        int right = array.length-1;
        while(left <= right){
            int mid = (left + right)/2;
            if(key == array[mid]) {
                return mid;
            }else if(key > array[mid]){
                left = mid + 1;
            }else {
                right = mid -1;
            }
        }
        return -1;
    }
    public static void main9(String[] args) {
        //二分查找-->有序的
        /*int[] array = {1,2,3,4,5};
        int index = binarySearch(array,4);//返回下标-->3
        System.out.println(index);*/
        //二分查找-->无序的
        int[] array1 = {3,5,2,5,6,1};
        System.out.println("排序前："+ Arrays.toString(array1));
        Arrays.sort(array1);//默认从小到大排序
        System.out.println("排序后："+ Arrays.toString(array1));
        int index1 = Arrays.binarySearch(array1,1,2,4);//[1,3)
        System.out.println(index1);

        //查找
        /*int ret = search(array,4);//返回下标-->3
        System.out.println(ret);*/
    }
    public static String myToString(int[] array) {
        String str = "[";
        for (int i = 0; i < array.length; i++) {
            str += array[i];
            if(i != array.length-1){
                str += ", ";
            }
        }
        str += "]";
        return str;
    }
    public static void main8(String[] args) {
        //数组转字符串
        int[] array = {1,2,3,4};
        String str = Arrays.toString(array);
        System.out.println(str);
        String str1 = myToString(array);
        System.out.println(str1);
    }
    public static void fuc2(int[] array) {
        array = new int[]{5,6,7,8,9};//形参array新指向了一个对象
        //一个引用不可能同时指向两个对象
    }
    public static void main7(String[] args) {
        int[] array = {1,2,3,4};
        fuc2(array);
        System.out.println(Arrays.toString(array));
    }
    public static int[] fib(int n) {
        if(n <= 0){
            return null;
        }
        int[] array = new int[n];
        array[0] = array[1] = 1;
        for (int i = 2; i < n; i++) {
            array[i] = array[i-1] +array[i-2];
        }
        return array;
    }
    public static void main6(String[] args) {
        //获取斐波那契数列的前N项
        int[] array = fib(10);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }

    }
    public static void extend(int[] array) {
        //需求：把数组当中的每个数据 扩大2倍 -> 数组本身上扩大
        for (int i = 0; i < array.length; i++) {
            array[i] = array[i] * 2;
        }
    }

    public static int[] extend1(int[] array) {
        //需求：把数组当中的每个数据 扩大2倍 -> 不改变数组本身
        int[] tmp = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            tmp[i] = array[i] * 2;
        }
        return tmp;//数组作为函数的返回值
    }
    public static void main5(String[] args) {

        /*int[] array = {1,2,3,4,5};
        System.out.println(Arrays.toString(array));
        extend(array);
        System.out.println(Arrays.toString(array));*/

        int[] array = {1,2,3,4,5};
        int[] tmp = extend1(array);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(tmp));

    }
    public static void fuc1(int[] array) {
        array[2] = 5;
    }
    public static void main4(String[] args) {
        int[] array = {1,2,3,4};
        System.out.println(Arrays.toString(array));

        fuc1(array);
        System.out.println(Arrays.toString(array));
    }
    public static void fuc(int n) {
        n = 10;
        System.out.println(n);//10
    }
    public static void main3(String[] args) {
        //数组作为函数的参数-->1.参数传基本类型:形参的改变不会影响实参的改变
        int num = 0;
        fuc(num);
        System.out.println(num);//-
    }
    public static void main2(String[] args) {
        int[] array = null;//array这个引用 没有指向任何对象 null表示一个无效的内存位置 不能对这个内存进行任何的读写操作
        //System.out.println(array.length);//NullPointerException-->空指针异常
        System.out.println(array[0]);//NullPointerException-->空指针异常
    }
    public static void main1(String[] args) {
        int[] array1 = new int[5];//如果这个对象一直没有被引用 就会被JVM自动回收
        array1[0] = 1;
        array1[1] = 2;
        array1[2] = 3;
        array1[3] = 4;
        array1[4] = 5;

        int[] array2 = new int[5];
        array2[0] = 5;
        array2[1] = 4;
        array2[2] = 3;
        array2[3] = 2;
        array2[4] = 1;

        array2 = array1;//array2这个引用 指向array2这个引用 所指向的对象
        array1[0] = 10;//当两个引用 同时指向一个对象 通过任意一个引用 都能修改这个对象的值
        array2[2] = 20;
        for (int i = 0; i < array2.length; i++) {
            System.out.print(array2[i] + " ");
        }

    }
}
